package com.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.utils.DBUtility;

public class UpdateDataServlet extends HttpServlet {

		private Connection conn;
		private PreparedStatement pst;
		private ResultSet rs;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6300749440687870330L;

	/**
	 * Constructor of the object.
	 */
	public UpdateDataServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}



	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {

		this.doPost(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		
		System.out.println("[UpdateDataServlet]"+ request.getRemoteHost()+"is requesting...");
		
		String location = request.getParameter( "location" );
		String msg = request.getParameter("msg");
		String dial = request.getParameter("dial");
		String queryUID = request.getParameter("phoneNum");
		String time = request.getParameter("time");
		
		if(location == null)location = "";
		if(msg == null)msg = "";
		if(dial == null)dial = "";
		if(time == null)time = "";
		
		location = new String(location.getBytes("ISO8859-1"),"UTF-8");
		msg = new String(msg.getBytes("ISO8859-1"),"UTF-8");
		dial = new String(dial.getBytes("ISO8859-1"),"UTF-8");
		
		System.out.println("UTF-8:" + location);
		System.out.println("UTF-8:" + msg);
		System.out.println("UTF-8:" + dial );
		
		
		if( location !=null && msg != null  && dial != null && queryUID != null ){
			
			if( location.equals("") && msg.equals("")  && dial.equals("") ){
				System.out.println("[UpdateDataServlet]the whole datas are null cannot upload inside DB");
			}else{
				updateDataToMyDB( queryUID, msg, dial, location, time );
				System.out.println("[UpdateDataServlet]the whole datas are not null can upload inside DB right now");
			}
		
			
		}else{
		}
		

		
	}
	
	/**
	 * @param queryUID
	 * @param msg
	 * @param dial
	 * @param location
	 * @return
	 */
	private int updateDataToMyDB( String queryUID, String msg, String dial, String location, String add_time ){
		int i = 0;
		String sql = "INSERT INTO db.jiankong_data_info_table (msg, dial, location, controlled_id, TYPE,add_time) VALUES(?, ?, ?, ?, 'new',?)";
	
		conn = DBUtility.getMySqlConnection();
		
		try {
			pst = conn.prepareStatement( sql );
			
			pst.setString( 1, msg );
			pst.setString( 2, dial );
			pst.setString( 3, location );
			pst.setString( 4, queryUID );
			pst.setString( 5, add_time );
			
			i = pst.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
			closeAllConnections();
			
		}
		
		return i;
	}

	
	
	/**
	 */
	private void closeAllConnections(){

		if( rs != null ){

			try {
				rs.close();

				rs = null;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		if( pst != null){

			try {
				pst.close();

				pst = null;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}



	}
	

	
	
	
	public void init() throws ServletException {
		// Put your code here
	}

}
